
#include <stdexcept>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/throw_exception.hpp>
#include "Plan.hpp"
using namespace boost;

namespace fixstoker {

Plan::Plan() {

}

Plan::~Plan() {
	for (std::vector<const PlanEntry*>::const_iterator it = entries_.begin(); it != entries_.end(); it++) {
		const PlanEntry* entry = *it;
		delete entry;
	}
	entries_.clear();
}

void
Plan::addMessageTemplate(const std::string& messageTemplate) {
	this->messageTemplates_.push_back(messageTemplate);
}

void
Plan::addEntry(const PlanEntry* entry) {
	if (entry->msgTemplateIndex() >= this->messageTemplates_.size()) {
		BOOST_THROW_EXCEPTION(std::invalid_argument(std::string("Invalid argument:")
				+ " entry->msgTemplateIndex() > this->messageTemplates_.size()"
				+ ", entry->msgTemplateIndex(): " + lexical_cast<std::string>(entry->msgTemplateIndex())));

	}
	this->entries_.push_back(entry);
}

int
Plan::numberOfMessageTemplates() const {
	return messageTemplates_.size();
}

const std::string
Plan::messageTemplate(const int connectionIndex) const {
	return messageTemplates_[connectionIndex];
}

const std::vector<const PlanEntry*>&
Plan::entries() const {
	return entries_;
}

}
